<?php

/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2019/3/14
 * Time: 2:00 PM
 */
class StrUtil
{
    const MAX_STR_LEN = 1000000;//最大字串长度
    const MAX_INT = 2147483647;//最大整数
    const MIN_INT = -2147483648;//最小整数
    const HexString = 1;    //0-9 a-f A-F
    const LowHexString = 2;
    const UpperHexString = 3;
    const DigistString = 4;
    const AlphaString = 5;// a-z A-Z
    const ALnumString = 6;//a-z A-Z 0-9
    const UinString = 7;// a-z A-Z 0-9 _ 用户ID
    const EmailString = 8;///^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/ email
    const MobileString = 9;///^0?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/ mobile
    const TelString = 10;// /^([0-9]{3,4}-)?[0-9]{7,8}$/ telephone

    public static function isStrValid($str, $type)
    {
        $ret = false;
        switch ($type) {
            case self::HexString:
                $ret = preg_match('/^[0-9a-fA-F]*$/', $str);
                break;
            case self::LowHexString:
                $ret = preg_match('/^[0-9a-f]*$/', $str);
                break;
            case self::UpperHexString:
                $ret = preg_match('/^[0-9A-F]*$/', $str);
                break;
            case self::DigistString:
                $ret = preg_match('/^[0-9]*$/', $str);
                break;
            case self::AlphaString:
                $ret = preg_match('/^[a-zA-Z]*$/', $str);
                break;
            case self::ALnumString:
                $ret = preg_match('/^[a-zA-Z0-9]*$/', $str);
                break;
            case self::UinString:
                $ret = preg_match('/^[a-zA-Z0-9][a-zA-Z0-9_]*$/', $str);
                break;
            case self::EmailString:
                $ret = preg_match(
                    '/^(\w)+(\.\w+)*(\-\w+)*@(\w)+((\.\w+)+)$/', $str
                );
                break;
            case self::MobileString:
                $ret = preg_match(
                    '/^0?(10[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9]|14[0-9])[0-9]{8}$/',
                    $str
                );
                break;
            case self::TelString:
                $ret = preg_match('/^[0-9]{3,4}-[0-9]{7,8}$/', $str);
                break;
            default:
                return false;
        }

        return $ret;

    }
}